<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginFormRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到贝贝甄选</h2>
          <!-- 账号 -->
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              :show-password="true"
            ></el-input>
          </el-form-item>
          <!-- 登录按钮 -->
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
// ele弹框
import { ElNotification } from "element-plus";
const router = useRouter();
const route = useRoute();
// 引入获取当前时间的函数
import { getTime } from "@/utils/time";
// 引入用户相关的小仓库
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
// 获取el-form组件
const loginFormRef = ref();
// 定义变量控制按钮加载效果
const loading = ref(false);
// 收集账号与密码数据
const loginForm = reactive({ username: "admin", password: "111111" });

// 登录按钮回调
const login = async () => {
  // 保证全部的表单项校验通过 才可以发送请求
  await loginFormRef.value.validate();

  // 展示加载效果
  loading.value = true;
  // 点击登录按钮以后干什么?
  // 通知仓库发送登录请求
  // 请求成功=>首页
  // 请求失败=>弹框提示登录失败
  try {
    // 保证登录成功
    await userStore.userLogin(loginForm);
    // 判断登录时路由是否携带query参数
    const redirect: any = route.query.redirect;
    // 编程式导航跳转首页
    router.push({ path: redirect || "/" });
    // 登录成功的提示信息
    ElNotification({
      type: "success",
      title: `Hi!,${getTime()}好`,
      message: "欢迎回来",
    });
    // 登录成功加载效果消失
    loading.value = false;
  } catch (error) {
    // 登录失败加载效果消失
    loading.value = false;
    // 登录失败的提示信息
    ElNotification({
      type: "error",
      message: (error as Error).message,
    });
  }
};

// 自定义校验规则
const validatorUserName = (rule: any, value: any, callback: any) => {
  // rule即为校验规则对象
  // value即为用户输入的值
  // 函数:如果符合规则 则通过callback放行 不通过则callback返回错误信息
  if (value.length < 5 || value.length > 10) {
    callback(new Error("账号长度在5到10位"));
  } else {
    callback();
  }
};
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length < 6 || value.length > 15) {
    callback(new Error("密码的长度在6到15位"));
  } else {
    callback();
  }
};

// 定义表单校验规则
const rules = {
  username: [
    // 规则对象属性说明
    // required:是否必须校验
    // message:校验失败的提示信息
    // trigger:触发方式
    // {
    //   required: true,
    //   min: 5,
    //   max: 10,
    //   message: "账号长度在5到10位",
    //   trigger: "change",
    // },
    { trigger: "change", validator: validatorUserName },
  ],
  password: [
    // {
    //   required: true,
    //   min: 6,
    //   max: 15,
    //   message: "密码的长度在6到15位",
    //   trigger: "change",
    // },
    { trigger: "change", validator: validatorPassword },
  ],
};
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
